package com.sx.gongzhong.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.widget.LockPatternView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 手势密码
 * Created by admin on 2018/1/2.
 */

public class HandPasswordActivity extends BaseActivity implements LockPatternView.OnPatternListener {
    @BindView(R.id.lock_pattern)
    LockPatternView mLockPatternView;
    @BindView(R.id.tv_SetPassWord)
    TextView tvSetPassWord;
    @BindView(R.id.small_lock_pattern)
    LockPatternView smallLockPattern;

    private boolean mConfirm = false;
    private int mStep;
    private static final int STEP_1 = 1; // 开始
    private static final int STEP_2 = 2; // 第一次设置手势完成
    private static final int STEP_3 = 3; // 第二次设置手势完成
    private List<LockPatternView.Cell> mChoosePattern = null;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_handpassword);
        setTitleName("设置手势密码");
        setTitleRight("确定");
        ButterKnife.bind(this);
        smallLockPattern.disableInput();
        smallLockPattern.setmPatternInProgress(true);
        smallLockPattern.setmAllowDrawPath(false);
        mLockPatternView.setOnPatternListener(this);
    }

    @OnClick({R.id.tv_clear_PassWord, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_clear_PassWord:
                mChoosePattern = null;
                smallLockPattern.clearPattern();
                mLockPatternView.clearPattern();
                mLockPatternView.enableInput();
                tvSetPassWord.setText("绘制手势图案");
                tvSetPassWord.setTextColor(getResources().getColor(R.color.c_a5a5a5));
                break;
            case R.id.tv_right:
                if (mConfirm) {

                } else {
                    ToastUtils.showToast(HandPasswordActivity.this, "请设置正确手势密码");
                }
                break;
        }
    }

    @Override
    public void onPatternStart() {
    }

    @Override
    public void onPatternCleared() {
    }

    @Override
    public void onPatternCellAdded(List<LockPatternView.Cell> pattern) {
    }

    @Override
    public void onPatternDetected(List<LockPatternView.Cell> pattern) {
        if (pattern.size() < LockPatternView.MIN_LOCK_PATTERN_SIZE) {
            tvSetPassWord.setText("最少连接4个点,请重新输入");
            tvSetPassWord.setTextColor(getResources().getColor(R.color.red));
            mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
            mStep = STEP_1;
            updateView();
            return;
        }
        if (mChoosePattern == null) {
            mChoosePattern = new ArrayList<>(pattern);
            mStep = STEP_2;
            updateView();
            return;
        }
        if (mChoosePattern.equals(pattern)) {
            mConfirm = true;
        } else {
            tvSetPassWord.setText("与上一次绘制不一致，请重新绘制");
            tvSetPassWord.setTextColor(getResources().getColor(R.color.red));
            mConfirm = false;
        }
        mStep = STEP_3;
        updateView();
    }

    private void updateView() {
        switch (mStep) {
            case STEP_1://开始
                mChoosePattern = null;
                mConfirm = false;
                mLockPatternView.clearPattern();
                mLockPatternView.enableInput();
                break;
            case STEP_2://第一次手势完成
                smallLockPattern.setPattern(LockPatternView.DisplayMode.Correct,mChoosePattern);
                tvSetPassWord.setText("再次绘制手势图案");
                tvSetPassWord.setTextColor(getResources().getColor(R.color.c_a5a5a5));
                mLockPatternView.clearPattern();
                mLockPatternView.enableInput();
                break;
            case STEP_3://第二次手势完成
                mLockPatternView.clearPattern();
                if (mConfirm) {
                    mLockPatternView.enableInput();
                    LockPatternView.patternToString(mChoosePattern);
                    ToastUtils.showToast(HandPasswordActivity.this, "设置完成");
                    finish();
                }
//                else {
//                    mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
//                    mLockPatternView.enableInput();
//                    mStep = STEP_1;
//                    updateView();
//                }
                break;
            default:
                break;
        }
    }
}
